package com.github.lee.unsplash.utils

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import coil.size.Scale
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import com.github.lee.core.common.dp2px
import com.github.lee.core.common.view.RatioImageView
import xyz.belvi.blurhash.BlurHash

@BindingAdapter(
    value = [
        "remote_url",
        "crossfade_duration",
        "placeholder", "error",
        "isCircle",
        "top_left", "top_right", "bottom_left", "bottom_right"
    ], requireAll = false
)
fun loadUrl(
    v: ImageView,
    url: String?,
    crossfadeDuration: Int?,
    placeholder: Drawable?,
    error: Drawable?,
    isCircle: Boolean? = false,
    topLeft: Float?,
    topRight: Float?,
    bottomLeft: Float?,
    bottomRight: Float?
) {
    if (url.isNullOrEmpty()) {
        return
    }
    v.load(url) {
        if (null == crossfadeDuration || crossfadeDuration <= 0) {
            crossfade(false)
        } else {
            crossfade(true)
            crossfade(crossfadeDuration)
        }
        if (null != placeholder) {
            placeholder(placeholder)
        }
        if (null != error) {
            error(error)
        }
        if (isCircle == true) {
            transformations(CircleCropTransformation())
        } else {
            val tl = dp2px(v.context, topLeft ?: 0F)
            val tr = dp2px(v.context, topRight ?: 0F)
            val bl = dp2px(v.context, bottomLeft ?: 0F)
            val br = dp2px(v.context, bottomRight ?: 0F)
            transformations(RoundedCornersTransformation(tl, tr, bl, br))
        }
    }
}


@BindingAdapter(
    value = [
        "remote_url",
        "width", "height",
        "crossfade_duration",
        "blurHash", "error",
        "isCircle",
        "top_left", "top_right", "bottom_left", "bottom_right"
    ], requireAll = false
)
fun loadUrlBlurHash(
    v: RatioImageView,
    url: String?,
    width: Int?,
    height: Int?,
    crossfadeDuration: Int?,
    blurHash: String?,
    error: Drawable?,
    isCircle: Boolean? = false,
    topLeft: Float?,
    topRight: Float?,
    bottomLeft: Float?,
    bottomRight: Float?
) {
    if (url.isNullOrEmpty()) {
        return
    }
    if (null == width || null == height || width == 0 || height == 0) {
        v.ration = 0F
        return
    }
    val ration = width * 1.0F / height
    v.ration = ration

    if (blurHash.isNullOrEmpty()) {
        loadUrl(
            v,
            url,
            crossfadeDuration,
            null,
            error,
            isCircle,
            topLeft,
            topRight,
            bottomLeft,
            bottomRight
        )
        return
    }
    val bh = BlurHash(v.context, 20, 1.0F)
    bh.execute(blurHash, 100, (100 / ration).toInt()) { bd ->
        bh.clean()
        v.load(url) {
            if (null == crossfadeDuration || crossfadeDuration <= 0) {
                crossfade(false)
            } else {
                crossfade(true)
                crossfade(crossfadeDuration)
            }
            placeholder(bd)
            if (null != error) {
                error(error)
            }
            if (isCircle == true) {
                transformations(CircleCropTransformation())
            } else {
                val tl = dp2px(v.context, topLeft ?: 0F)
                val tr = dp2px(v.context, topRight ?: 0F)
                val bl = dp2px(v.context, bottomLeft ?: 0F)
                val br = dp2px(v.context, bottomRight ?: 0F)
                transformations(RoundedCornersTransformation(tl, tr, bl, br))
            }
        }
    }

}